package oceanwiki.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import org.springframework.context.annotation.Bean;

import java.util.ArrayList;
import java.util.List;

/**
 * @FileName CopyUtil
 * @Description
 * @Author Me
 * @date 2024-09-10
 **/

public class CopyUtil {
    //单体复制
    public static <T> T copy(Object source, Class<T> tClass) {
        if(source == null) {
            return null;
        }
        T obj = null;

        try {
            obj = tClass.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
            return null;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        BeanUtil.copyProperties(source, obj);
        return obj;
    }

    //列名复制
    public static <T> List<T> copyList(List source, Class<T> tClass) {
        List<T> target = new ArrayList<>();
        if (!CollectionUtil.isEmpty(source)){
            for (Object o: source) {
                T obj = copy(o, tClass);
                target.add(obj);
            }
        }
        return target;
    }
}
